//
//  UndistributeCtry.m
//  ZHZtest
//
//  Created by 李彦宏 on 2018/1/12.
//  Copyright © 2018年 LYH. All rights reserved.
//未分配研究生的学员

#import "UndistributeCtry.h"
#import "DistributStuHead.h"//头部
#import "UndistributeCell.h"//未分配cell
#import "JWUserManager.h"//教务端管理
#import "SDUserModel.h"//学生model
#import "JWGRUsermodel.h"//研究生model
#import "JWChooseGrSeachVC.h"//研究生选择搜索

@interface UndistributeCtry ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *UndistributeTab;
@property (nonatomic, strong) SDUserModel *model;
@property (nonatomic, strong) JWGRUsermodel *Gmodel;
@property (nonatomic, strong) NSMutableArray *SarryList;
@property (nonatomic, strong) NSMutableArray *GRarrylist;
@property (nonatomic, assign) NSInteger  index;
@end
@implementation UndistributeCtry



- (void)setupTable
{

    //UItableView初始化
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    //隐藏组距导航栏的间距
//    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    tableView.tableFooterView=[[UIView alloc]init];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tableView];
    self.UndistributeTab=tableView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated ];
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [EasyShowLodingView hidenLoding];
        [self Studentdate];
    });
    
    NSLog(@"viewWillAppear");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.model=[JWUserManager SDme];
    self.Gmodel=[JWUserManager Gme];
    //加载研究生列表
    [self setGRdate];
    // Do any additional setup after loading the view.
}

#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _SarryList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UndistributeCell *cell=[UndistributeCell cellwithTableview:tableView];
    
    [cell setDisChooseBlock:^{
        JWChooseGrSeachVC *vc=[[JWChooseGrSeachVC alloc]init];
        
        vc.tagsArray=[[NSMutableArray alloc]init];
        if(self.GRarrylist.count<7){
            vc.tagsArray=[NSMutableArray arrayWithObjects:@"李彦宏",@"朱圳",@"严戈闲",@"赵延",@"田涛",@"肖欢欣",@"游鹤亭",@"陈炽", nil];
        }else{
            for (int i=0; i<7; i++) {
                self.Gmodel=self.GRarrylist[i];
                [vc.tagsArray  addObject:self.Gmodel.name];
            }
        }
        self.model=self.SarryList[indexPath.row];
        [JWUserManager saveSD:self.model];
        [self.navigationController pushViewController:vc animated:YES];
    }];

    cell.SDmodel=self.SarryList[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.UndistributeTab deselectRowAtIndexPath:indexPath animated:YES];
     self.model=_SarryList[indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return  40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    DistributStuHead *head=[[DistributStuHead alloc]init];
    
    return head;

}
#pragma -mark ——————————————————获取学生信息————————————————————
-(void)Studentdate{
    NSDictionary *dict=@{
                         @"jwid" : JWid,
                         @"jwpassword" : JWpassword,
                         };
    [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_Undistributed parmas:dict successData:^(id json) {
        [self.SarryList removeAllObjects];
        NSArray * arr = json[@"data"];
                __weak typeof(self) weakslf = self;
        
                for(int i=0;i<arr.count;i++){
                    self.model = [SDUserModel yy_modelWithJSON:arr[i]];
                    [JWUserManager saveSD:self.model];
                    [weakslf.SarryList addObject: weakslf.model ];
                }
        dispatch_async(dispatch_get_main_queue(), ^{
            if(arr.count==0){
              KongDataView  *Kview=[KongDataView NilDataview];
                self.view.frame= Kview.frame;
                [self.view addSubview:Kview];
            }else{
                [self setupTable];
            }
            
            [weakslf.UndistributeTab reloadData];
                });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
}
#pragma -mark ————————————————研究生数据————————————————————
-(void)setGRdate{
    
    __weak typeof(self) weakslf = self;
    
    NSDictionary *dict=@{
                         @"jwid":JWid,
                         @"jwpassword":JWpassword,
                         @"page" :@"1",
                         };
    NSLog(@"--dict--%@", dict);
    //    参数  分页=>page 教务id=〉jwid ，秘码=>jwpassword
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_ChangeGR parame:dict successData:^(id json) {
        [self.GRarrylist removeAllObjects];
        NSArray * arr = json[@"data"];
        
        for(int i=0;i<arr.count;i++){
            
            self.Gmodel = [JWGRUsermodel yy_modelWithJSON:arr[i]];
            [JWUserManager saveGR:self.Gmodel];
            
            [weakslf.GRarrylist addObject: self.Gmodel ];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.UndistributeTab reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
}
-(NSMutableArray *)SarryList{
    if(_SarryList==nil){
        _SarryList=[[NSMutableArray alloc]init];
    }
    return _SarryList;
}
-(NSMutableArray*)GRarrylist{
    if(_GRarrylist==nil){
        
        _GRarrylist=[[NSMutableArray alloc]init];
    }
    return _GRarrylist;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
